<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/bean/Abstract.php");
require_once("cms/menu/om/MenuPeer.php");
require_once("cms/menu/om/MenuItemPeer.php");
require_once("cms/menu/om/MenuItemI18nPeer.php");

class PHP_CMS_View_Helper_Menuitems extends AbstractList {

    private $menu = null;

    public function menuitems($form=null) {

        if($this->isInit)
            return $this;

        parent::init($form);

        $this->criteria->add(MenuItemPeer::MENU_ID,
            $form->getId());

        $this->criteria->addAlias("join2", MenuItemPeer::TABLE_NAME);

        $this->criteria->addAsColumn(
            'count', 'COUNT(join2.menu_item_id)');

        $this->criteria->addGroupByColumn(
            "menu_item.menu_item_id");

        $this->criteria->addJoin(MenuItemI18nPeer::MENU_ITEM_ID,
            MenuItemPeer::MENU_ITEM_ID, Criteria::LEFT_JOIN);

        $this->criteria->addJoin(MenuItemPeer::MENU_ITEM_ID,
            "join2.parent_id", Criteria::LEFT_JOIN);

        if($form->getOrder() == MenuItemPeer::POSITION) {
            $this->form->setDirection("asc");
        }
            
        if(strlen($form->getLanguage()) > 0)
            $this->criteria->add(MenuItemI18nPeer::LANGUAGE_CODE,
                $form->getLanguage());

        if(strlen($form->getQuery()) == 0 && strlen($form->getLanguage()) == 0) {

            $this->criteria->add(
                MenuItemI18nPeer::LANGUAGE_CODE,
                $this->getMenu()->getLanguageCode()
            );

            $this->criteria->add(MenuItemPeer::PARENT_ID,
                $form->getParentId() > 0 ? $form->getParentId() : null);

        }

        $this->criteria->setDistinct();

        return $this;
    }


    public function getMenu() {
        if($this->menu == null)
            $this->menu = MenuPeer::retrieveByPk($this->form->getId());
        return $this->menu;
    }


    public function getName() {
        if($this->form->getParentId() == 0) {
            return $this->getMenu()->getName();
        } else {

            $sql = "SELECT menu_item_i18n.name FROM menu_item_i18n " .
                   "LEFT JOIN menu_item USING (menu_item_id) " .
                   "LEFT JOIN menu USING (menu_id) " .
                   "WHERE menu_item_i18n.language_code = menu.language_code " .
                   "AND menu_item_i18n.menu_item_id = ".$this->form->getParentId() . " " .
                   "LIMIT 1";

            $conn = Propel::getConnection();
            $stmt = $conn->createStatement();
            $rs   = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);

            if($rs->next())
                return $rs->getString(1);
        }
    }


    /**
     * Define the propel peer we are going to use to list
     * items.
     */
    public static function getPeer() {
        return new MenuItemI18nPeer();
    }


    /**
     * Set the searchable fields for this list page.
     */
    public static function getFields() {
        return array(
            MenuItemI18nPeer::NAME,
            MenuItemI18nPeer::INSERT_DATE,
            MenuItemI18nPeer::MODIFIED_DATE,
            MenuItemPeer::POSITION
        );
    }
}
?>